<?php

namespace Biz\model\brandActivity;

use Doraemon\model\Phone as PhoneModel;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\tools\Log;
use hhz\constant\basic\UserExtendPhoneScene;

class BlindBoxLotteryConsignee extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'blind_box_lottery_consignee';

    const CONSIGNEE_NORMAL = 0;//未发货
    const CONSIGNEE_TRANSITING = 1;//运输中
    const CONSIGNEE_SUCCESS = 2;//已签收

    public static function getByWhere($where, $select = '*')
    {
        return self::get($select, $where);
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);
        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        if ($data['consignee_mobile']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['consignee_mobile'], UserExtendPhoneScene::BLIND_BOX_LOTTERY_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['consignee_mobile_encrypt_id'] = $encryptPhoneId;
            $data['consignee_mobile'] = PhoneCrypto::mask($data['consignee_mobile']);
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        if ($data['consignee_mobile']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($data['consignee_mobile'], UserExtendPhoneScene::BLIND_BOX_LOTTERY_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$data['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $data['consignee_mobile_encrypt_id'] = $encryptPhoneId;
            $data['consignee_mobile'] = PhoneCrypto::mask($data['consignee_mobile']);
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

    /**
     * @param array $ids
     * @return array
     */
    public static function getByLotteryIds(array $lotteryIds): array
    {
        if (!$lotteryIds) {
            return [];
        }

        $map = ['lottery_id' => $lotteryIds];
        return self::select('*', $map);
    }
}